﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IFServerDLL;
using IFServerDLL.Model;
using System.ComponentModel;
using System.Threading;

namespace IFClientDLL
{
    public class Rsp : IResponeData
    {
        private ITryIF _tryif;
        BackgroundWorker _bw;
        public Rsp(ITryIF tryif ,BackgroundWorker bw)
        {
            _bw = bw;
            _bw.WorkerReportsProgress = true;
            _bw.ProgressChanged += new ProgressChangedEventHandler(_bw_ProgressChanged);
            _bw.DoWork += new DoWorkEventHandler(_bw_DoWork);
            _bw.RunWorkerAsync();
            _tryif = tryif;
        }

        void _bw_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                Thread.Sleep(5000);
            }
        }

        void _bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 1)
            {
                OrderBase order = e.UserState as OrderBase;
                _tryif.Client.OnGetOrder(order);
            }
            else if (e.ProgressPercentage == 2)
            {
                TradeBase trade = e.UserState as TradeBase;
                _tryif.Client.OnGetTrade(trade);
            }
            else if (e.ProgressPercentage == 3)
            {
                MessageBase msg = e.UserState as MessageBase;
                _tryif.AddMessage(msg);
            }
            else
            {

            }



        }
        public void RspOrder(OrderBase order)
        {
            _bw.ReportProgress(1, order);
           
        }

        public void RspTrade(TradeBase trade)
        {
            _bw.ReportProgress(2, trade);
        }

        public void RspMsg(MessageBase msg)
        {
            _bw.ReportProgress(3, msg);
        }
    }
}
